package com.mastercard.assets.web.util.to;

import com.mastercard.assets.MasterCardException;
import com.mastercard.assets.jpa.TblRole;
import com.mastercard.assets.jpa.TblUsuario;
import com.mastercard.assets.jpa.TblPaises;
import com.mastercard.assets.util.dao.MasterCardDAO;

public class UserTO
	implements
		java.io.Serializable
{
	private String login = "" ;
	private String name = "" ;
	private String pass = "" ;
	private String email = ""  ;
	private TblPaises pais = new TblPaises()  ;
	
	private int[ ] rolesId = new int[ 0 ] ;
	private TblRole[ ] roles = new TblRole[ 0 ] ;
	
	private int[ ] levels ;
        
        public TblPaises getPais() {
		return pais;
	}

	public void setPais(TblPaises pais) {
		if( pais != null )
			this.pais = pais;
	}
        
	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		if( login != null )
			this.login = login;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		if( name != null )
			this.name = name;
	}

	public String getPass() {
		return pass;
	}

	public void setPass(String pass) {
		if( pass != null )
			this.pass = pass;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		if( email != null )
			this.email = email;
	}

	public int[] getRolesId() {
		return rolesId;
	}
	
	public void loadRoles( )
	{
		// en caso de no null
		if( rolesId == null )
			return ;
		
		// en caso de que no sea null
		// revisa si es 0 elementos
		if( rolesId.length == 0 )
			roles = new TblRole[ 0 ] ;
		// en caso de que no sea 0
		else
		{
			// crea los roles
			roles = new TblRole[ rolesId.length ] ;
			
			int index = 0 ;
			
			// recorre los rolesId
			for( int roleId : rolesId )
			{
				// toma el TblRole
				TblRole role = MasterCardDAO.findRoleById( roleId ) ;
				
				// agrega
				roles[ index++ ] = role ;
			}
		}
                MasterCardDAO.closeEntityManager( ) ;		
	}

	public void setRolesId(int[] rolesId) {
		if( rolesId != null )
		{
			this.rolesId = rolesId;
			this.roles = new TblRole[ rolesId.length ] ;
		}
	}

	public TblRole[] getRoles( )
	{
		return roles;
	}
	
	public void setLevels( int[ ] levels )
	{
		this.levels = levels ;
	}
	
	public int registerUser( )
		throws
			MasterCardException
	{
		// agrega el usuario co nlos niveles y roles
		MasterCardDAO.addNewUser( login , email , name , pass , rolesId , levels , pais ) ;
		
		TblUsuario user = MasterCardDAO.findUserByLogin( login ) ;
		
                MasterCardDAO.closeEntityManager( ) ;
		if( user == null )
			return -1 ;
		else
			return user.getCveUsuario( ) ;
	}
}
